/**
 * 字符串操作工具类
 */
import BaseClass from "../base/BaseClass"

export default class StringUtils extends BaseClass<StringUtils> {

    /**
     * 去掉前后空格
     * @param str
     * @returns {string}
     */
    public trimSpace(str: string): string {
        return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1')
    }

    /**
     * 获取字符串长度，中文为2
     * @param str
     */
    public getStrLen(str: string): number {
        let strArr = str.split("")
        let length = 0
        for (let i = 0; i < strArr.length; i++) {
            let s = strArr[i]
            if (this.isChinese(s)) {
                length += 2
            } else {
                length += 1
            }
        }
        return length
    }

    /**
     * 判断一个字符串是否包含中文
     * @param str
     * @returns {boolean}
     */
    public isChinese(str: string): boolean {
        let reg = /^.*[\u4E00-\u9FA5]+.*$/
        return reg.test(str)
    }

    public Format(str: string, ...val: string[]): string {
        for (let index = 0; index < val.length; index++) {
            if (val[index]) { str = str.replace(`{${index}}`, val[index]) }
        }
        return str
    }
}